<!--
  - Copyright (c) 2018.
  - 北京九思金信科技有限公司对该文件内容保留所有权利。
  - 未经许可，不得私自拷贝、传播、发布、引用该文件内容，违者将被追究法律责任。
  -->

<template>
  <div>
    <el-row>
      <el-col :span="4" align="left" style="line-height: 36px;height: 36px">
        <span style="font-size: 14px;color: #747778">实际控制人信息</span>
      </el-col>
    </el-row>
    <el-row style="justify-content: center">
      <el-button @click="addRow()"
                 style="margin: 8px 0px 8px 0px;padding: 6.2px 7px;font-size: 13px;line-height: 18px"
                 v-show="tableData.length === 0 && canEdit  && editable" round>
        <i class="joys-icon-new" style="margin-right: 12px;margin-left: 4px"/>
        <span style="margin: 0px">添加实际控制人信息</span>
      </el-button>
      <el-button type="primary"
                 @click="addRow()"
                 style="padding: 0px 18px 0px 8px;line-height: 36px;margin: 12px 0px"
                 v-show="editable && canEdit && !showForm && tableData.length > 0" round>
        <i class="joys-icon-addagain" style="margin-right: 18px;"/>
        <span style="margin: 0px;display: inline-block;font-size: 14px;">继续添加</span>
      </el-button>
      <span v-if="tableData.length === 0 && !editable">没有数据</span>
    </el-row>

    <transition name="el-zoom-in-top">
      <el-form :model="formData" ref="data"
               :class="{cardBackColor:!canEdit}"
               :rules="rules"
               style="text-align: left;padding-top: 28px"
               v-show="showForm" inline>
        <el-row style="padding:0px 12px 24px 12px">
          <el-col :md="8" :lg="6">
            <el-form-item label="名称" prop="actualControllerName">
              <ft-input :editable="!canEdit"
                        v-model="formData.actualControllerName"
                        class="input"></ft-input>
            </el-form-item>
          </el-col>

          <el-col :md="8" :lg="6">
            <el-form-item label="客户类型" prop="actualControllerType">
              <ft-enum-select v-model="formData.actualControllerType"
                              service-name="lss-customer"
                              type-name="shareholderType"
                              @change="shareholderTypeChange($event,formData,formData.actualControllerType)"
                              :editing="!canEdit">
              </ft-enum-select>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="证件类型" prop="identityType">
              <ft-enum-select v-if="formData.actualControllerType == '1'"
                              v-model="formData.identityType"
                              :editing="!canEdit"
                              service-name="lss-customer"
                              type-name="personalType"/>
              <ft-enum-select v-else
                              v-model="formData.identityType"
                              service-name="lss-customer"
                              :editing="!canEdit"
                              type-name="certificateType"/>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="证件号码" prop="identityNo">
              <ft-input :editable="!canEdit" v-model="formData.identityNo" class="input"></ft-input>
            </el-form-item>
          </el-col>
          <el-col :md="24" :lg="24">
            <el-form-item label="备注" prop="remark">
              <ft-input type="textarea" :editable="!canEdit" :autosize='{ minRows: 1, maxRows: 5}' v-model="formData.remark"></ft-input>
            </el-form-item>
          </el-col>
        </el-row>

        <hr style="background-color: #EDEDED;border: none;height: 1px;margin: 0px"/>

        <el-row style="padding:16px 0px">
          <el-col align="right">
            <el-button size="mini"
                       @click="handleCancel()"
                       v-show="!canEdit">取消
            </el-button>
            <el-button size="mini"
                       type="primary"
                       @click="handleSave()"
                       style="margin-left: 24px;margin-right: 36px"
                       v-show="!canEdit">保存
            </el-button>
          </el-col>
          <el-col align="right" v-show="canEdit">
            <el-button size="mini"
                       style="margin-left: 24px;margin-right: 36px"
                       @click="handleCancel()">返回
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </transition>

    <transition name="el-zoom-in-top">
      <el-table v-show="tableData.length > 0 && !showForm"
                tooltip-effect="light"
                :data="tableData">
        <el-table-column label="名称" min-width="120px" prop="actualControllerName" align="left" show-overflow-tooltip>
          <template slot-scope="scope">
            <ft-link-text @click="handleCard(scope.$index,scope.row)"
                          :text="scope.row.actualControllerName">
            </ft-link-text>
          </template>
        </el-table-column>

        <el-table-column label="客户类型" min-width="120px" prop="actualControllerType" align="left">
          <template slot-scope="scope">
            <ft-enum-select v-model="scope.row.actualControllerType"
                            service-name="lss-customer"
                            type-name="shareholderType"
                            placeholder="请选择"
                            :editing="tableEnumEdit">
            </ft-enum-select>
          </template>
        </el-table-column>
        <el-table-column label="证件类型" min-width="130px" prop="identityType" align="left" show-overflow-tooltip>
          <template slot-scope="scope">
            <ft-enum-select v-if="scope.row.actualControllerType == '1'"
                            v-model="scope.row.identityType"
                            :editing="!canEdit"
                            service-name="lss-customer"
                            type-name="personalType"/>
            <ft-enum-select v-else
                            v-model="scope.row.identityType"
                            service-name="lss-customer"
                            :editing="!canEdit"
                            type-name="certificateType"/>
          </template>
        </el-table-column>
        <el-table-column label="证件号码" min-width="120px" prop="identityNo" align="left" show-overflow-tooltip></el-table-column>
        <el-table-column v-if="editable" label="操作" align="center" min-width="120px">
          <template slot-scope="scope">
            <el-button type="text"
                       size="small"
                       @click="handleEdit(scope.$index,scope.row)">编辑
            </el-button>
            <el-popover title="确认" placement="top" width="200" v-model="scope.row.delVisible">
              <p>数据删除后，将不能恢复，确认删除？</p>
              <div style="text-align:right; margin:0">
                <el-button size="small"
                           @click="scope.row.delVisible=false">取消
                </el-button>
                <el-button type="danger" size="small" @click="handleDelete(scope.$index, tableData)">确定</el-button>
              </div>
              <span slot="reference" style="margin-left:.10rem">
              <el-button type="text" size="small" style="color:#B94A48">删除
              </el-button>
            </span>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
    </transition>
  </div>
</template>

<script>
  import {identityFormat,identityCorpFormat} from "@/pages/pc/assets/js/validate/rule.js"
  import {ENTERPRISE,PERSONAL} from "@/pages/pc/assets/js/validate/constant"

  export default {
    name: "ActualController",
    props: {
      customerId: {
        type : String,
        default : '',
      },
      editable: {
        type : Boolean,
        default : true,
      },
    },
    mounted() {
      this.load()
    },
    data() {

      //根据证件号码判断是否合法
      let validateCustNum = (rule, value, callback) => {
        if (this.formData.actualControllerType==ENTERPRISE){
          if (!identityCorpFormat(this.formData.identityType, value)) {
            callback(new Error('输入的证件号码不合法'))
          }
          else {
            callback()
          }
        }else {
          if (!identityFormat(this.formData.identityType, value)) {
            callback(new Error('输入的证件号码不合法'))
          }
          else {
            callback()
          }
        }
      };

      return {
        identityEnum: 'personCertificateType',
        tableData: [],
        tableEnumEdit: false,
        dataVersion: "",
        shId: "",
        formData: {},
        showForm: {
          type: Boolean,
          default: false
        },
        canEdit: true,
        rules: {
          actualControllerName: [
            {required: true, message: '名称不能为空', trigger: 'blur'},
            {max: 80, message: '不能超过80字符', trigger: 'blur'}],

          actualControllerType: [
            {required: true, message: '客户类型不能为空', trigger: 'change'},],
          identityType: [
            {required: true, message: '证件类型不能为空', trigger: 'blur'},],
          identityNo: [
            {required: true, message: '证件号码不能为空', trigger: 'blur'},
            {max: 40, message: '不能超过40字符', trigger: 'blur'},
            {validator: validateCustNum, trigger: 'blur'}],

        },
      }
    },
    methods: {
      data() {
        return {}
      },
      load() {
        this.$http.get('/joys-rest/lss-customer/actualcontroller/' + this.customerId).then(
          resp => {
            this.tableData = resp.body;
            this.showForm = false
          },
          resp => {
            this.$message.error(resp.body)
          })
      },
      //列表删除按钮
      handleDelete(index, row) {
        this.shId = row[index].actualControllerId;
        this.$http.delete('/joys-rest/lss-customer/actualcontroller/' + this.shId).then(
          resp => {
            row.splice(index, 1);
            this.$message.success('删除成功!')
          },
          resp => {
            this.$message.error(resp.body)
          })
      },
      //列表卡片按钮
      handleCard(index, row) {
        this.formData = row;
        this.canEdit = true;
        this.showForm = true
      },
      //列表取消
      handleCancel() {
        this.showForm = false;
        this.canEdit = true;
        this.$refs.data.clearValidate()
      },
      //列表编辑按钮
      handleEdit(index, row) {
        this.formData = JSON.parse(JSON.stringify(row));
        this.canEdit = false;
        this.showForm = true
      },
      //列表增行
      addRow() {
        this.formData = {
          customerId: this.customerId,

        };
        this.index = -1;
        this.canEdit = false;
        this.showForm = true
      },
      //保存
      handleSave() {
        this.$refs.data.validate((valid) => {
          if (valid) {
            this.$http.put('/joys-rest/lss-customer/actualcontroller/', this.formData).then(
              resp => {
                this.load()
                this.canEdit = true;
                this.showForm = false
              },
              resp => {
                this.$message.error(resp.body);
                this.canEdit = false
              })
          } else {
            return false
          }
        })
      },
      //客户类型枚举值变化
      shareholderTypeChange($event, row, val) {
        if (val == ENTERPRISE) {
          this.identityEnum = "corpCertificateType";
          this.canEdit = true;
          this.canEdit = false
        } else {
          this.identityEnum = "personCertificateType";
          this.canEdit = true;
          this.canEdit = false
        }
        this.$set(this.formData, 'identityType', '');
        this.$set(this.formData, 'identityNo', '');
        this.$refs.data.clearValidate(['identityType', 'identityNo'])

      },
      //字数较多时，超出部分用...显示
      convertInputValue(val){
        if(null != val && '' != val && val.length >9){
          return val.substr(0,9)+'...'
        }
        return val
      }
    },
  }
</script>
<style>

</style>
<style scoped>
  .input{
    overflow: auto;
    white-space: normal;
    word-wrap: break-word;
  }
</style>
